package cn.ekuma.android.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.ekuma.android.ui.adapter.InputViewAdapter;
import cn.ekuma.epos.ui.android.R;


public class InputView extends LinearLayout{
	
    String inputText;
    boolean isInputMode;
    InputViewAdapter inputAdapter;
	private TextView inputLabel;
	OnSelectedActionListener l;
	private ImageButton okSelect;
    
	public InputView(Context context) {
		super(context);
		init(context);
	}
	
	public InputView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
 
	
    private void init(final Context context) {
    	// ��ȡҳ�沼�� 
        View mainView = LayoutInflater.from(context).inflate( 
                        R.layout.input_view, this); 
        
          //ȡ��GridView����
      		GridView gridview = (GridView) findViewById(R.id.gridView1);
      		inputAdapter=new InputViewAdapter(context,gridview);
      		//���Ԫ�ظ�gridview
      		gridview.setAdapter(inputAdapter);

      		// ����Gallery�ı���
      		//gridview.setBackgroundResource(R.drawable.bg0);

      		//�¼�����
      		gridview.setOnItemClickListener(new OnItemClickListener() {
      			public void onItemClick(AdapterView<?> parent, View v, int position, long id)
      			{
      				cTran(inputAdapter.getcTran(position));
      			}
      		});
      	inputLabel=	(TextView) findViewById(R.id.textView1);
      		
      	ImageButton del=(ImageButton) findViewById(R.id.btnBack);
        del.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				if(isInputMode&&inputText.length()>0){
		              inputText=inputText.substring(0, inputText.length()-1);
		              inputLabel.setText(inputText);
		         }
			}
         });
        
        okSelect=(ImageButton) findViewById(R.id.btnOk);
        okSelect.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				onSelectOk();
			}
        });
        
        ImageButton ceSelect=(ImageButton) findViewById(R.id.btnCE);
        ceSelect.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				reset();
			}
        	
        });  
        reset();
	}


    public void onSelectOk(){
    	double retValue=0;
		if(inputText.length()>0){
              retValue=Double.valueOf(inputText);
         }
		if(l!=null)
			l.onOkAction(retValue);	
	}


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // Dispatch in the normal way
    	System.out.println("在dispatchKeyEventk你按下了(原值:"+event.getKeyCode());
        boolean handled = super.dispatchKeyEvent(event);
        if (!handled) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                // ... and our focused child didn't handle it
                // ... give it to ourselves so we can scroll if necessary
                handled = onKeyDown(event.getKeyCode(), event);
            }
        }
        return handled;
    }
    
    
	public void reset(){
        isInputMode=true;
        inputText="";
        resetText();
    }
    
    private void resetText() {
    	inputLabel.setText("");
	}

	private void cTran(String cTrans){
        if(!isInputMode){
            isInputMode=true;
            inputText="";
        }
        if (cTrans == "."&&!(inputText.length()==0)&&!(inputText.lastIndexOf(".")>0))
            inputText+=cTrans;
        if ((cTrans == "0" ||cTrans == "1" || cTrans == "2" || cTrans == "3" || cTrans == "4" || cTrans == "5" || cTrans == "6" || cTrans == "7" || cTrans == "8" || cTrans == "9") ) {
             inputText+=cTrans;
        }
        inputLabel.setText(inputText);
    }
	
	
	/**
     * Interface definition for a callback to be invoked when an action is
     * performed on the editor.
     */
    public interface OnSelectedActionListener {   
        void onOkAction(double value);
    }
    
    
    public void setOnSelectedActionListener(OnSelectedActionListener l){
    	this.l=l;
    }


	public void reset(double i) {
		reset();
		isInputMode=true;
		inputText=String.valueOf(i);
		inputLabel.setText(inputText);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		System.out.println("按下了(原值:"+keyCode);
		if (keyCode >= '0' && keyCode <= '9'){
			char value=(char)(keyCode - '0' + KeyEvent.KEYCODE_0);
			System.out.println("按下了:"+value);
			cTran(String.valueOf(value));
		}else if (keyCode == '\n')
			onSelectOk();
		else
			return super.onKeyDown(keyCode, event);
		return true;
	}
}
